package com.jumi.microservice.material.controller;

import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.HttpStatus;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.excel.utils.ExcelUtils;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.material.domain.JmWarehouse;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseRequest;
import com.jumi.microservice.material.dto.warehouse.JmWarehouseResponse;
import com.jumi.microservice.material.service.IJmWarehouseService;
import com.jumi.microservice.material.service.JmWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author ssk
 * @create 2020/7/21 9:19
 **/
@RestController
@RequestMapping("/jmWarehouse")
@Api(tags = {"【基础资料】仓库信息接口"})
@DubboService
public class JmWarehouseController extends BaseController implements IJmWarehouseService {

    @Resource
    private JmWarehouseService jmWarehouseService;

    @GetMapping("/findByPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "warehouseName", value = "仓库名称", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "linkName", value = "联系人名称", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "linkPhone", value = "联系人电话", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "status", value = "仓库状态", dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "startTime", value = "查询时间(起始时间)", dataType = "String", paramType = "query"),
                    @ApiImplicitParam(name = "endTime", value = "查询时间(截止时间)", dataType = "String", paramType = "query"),
            })
    public TableDataInfo<JmWarehouseResponse> findByPage(JmWarehouseRequest jmWarehouseReq) {
        startPage();
        List<JmWarehouseResponse> list = jmWarehouseService.findAll(jmWarehouseReq);
        return getDataTable(list);
    }

    @Override
    @GetMapping("/findById")
    @ApiOperation("根据Id查询仓库")
    @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = true, dataType = "Long")
    public ResponseResult<JmWarehouseResponse> findById(@NotNull(message = "仓库ID不能为空") Long warehouseId) {
        JmWarehouseResponse jmWarehouse = jmWarehouseService.findById(warehouseId);
        return ResponseResult.success(jmWarehouse);
    }

    @GetMapping("/export")
    @ApiOperation("导出excel表格")
    @Log(title = "导出excel表格",businessType = BusinessType.EXPORT)
    public void export(JmWarehouseRequest jmWarehouseReq, HttpServletResponse response) throws IOException {
        List<JmWarehouseResponse> list = jmWarehouseService.findAll(jmWarehouseReq);
        ExcelUtils<JmWarehouseResponse> util = new ExcelUtils<JmWarehouseResponse>(JmWarehouseResponse.class);
        util.exportExcel(response,list, "仓库信息");
    }


    @PostMapping("/add")
    @ApiOperation("新增仓库信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "warehouseName", value = "仓库名称", required = true, dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "linkName", value = "联系人名称", required = true, dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "linkPhone", value = "联系人电话", required = true, dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "provinceId", value = "仓库所在省名称", dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "cityId", value = "仓库所在市名称", dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "areaId", value = "仓库所在地区名称", dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "address", value = "详细地址", dataType = "String", paramType = "form")
            })
    @Log(title = "供应链管理-基础管理-仓库信息",businessType = BusinessType.INSERT)
    public ResponseResult add(@Validated JmWarehouseRequest jmWarehouseReq,@RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        JmWarehouse jmWarehouse = new JmWarehouse();
        jmWarehouseReq.clone(jmWarehouse);
        jmWarehouse.setCreateBy(createBy);
        int rows = jmWarehouseService.save(jmWarehouse);
        return rows == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "仓库名称已存在!") : ResponseResult.success(jmWarehouse.getWarehouseId());
    }

    @PostMapping("/edit")
    @ApiOperation("更新仓库信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "warehouseId", value = "仓库ID", required = true, dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "warehouseName", value = "仓库名称", required = true, dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "linkName", value = "联系人名称", required = true, dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "linkPhone", value = "联系人电话", required = true, dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "provinceId", value = "仓库所在省名称", dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "cityId", value = "仓库所在市名称", dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "areaId", value = "仓库所在地区名称", dataType = "Long", paramType = "form"),
                    @ApiImplicitParam(name = "address", value = "详细地址", dataType = "String", paramType = "form"),
                    @ApiImplicitParam(name = "status", value = "状态", dataType = "int",paramType = "form")
            })
    @Log(title = "供应链管理-基础管理-仓库信息",businessType = BusinessType.UPDATE)
    public ResponseResult edit(JmWarehouseRequest jmWarehouseReq,@RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        JmWarehouse jmWarehouse = new JmWarehouse();
        jmWarehouseReq.clone(jmWarehouse);
        jmWarehouse.setUpdateBy(updateBy);
        int rows = jmWarehouseService.update(jmWarehouse);
        return rows == 0 ? ResponseResult.error(HttpStatus.CONFLICT, "仓库名称已存在!") : ResponseResult.success(jmWarehouse.getWarehouseId());
    }

    @Override
    @ApiOperation("获取仓库接口")
    public ResponseResult<List<JmWarehouseResponse>> findAll() {
        JmWarehouseRequest jmWarehouseReq=new JmWarehouseRequest();
        List<JmWarehouseResponse> list = jmWarehouseService.findAll(jmWarehouseReq);
        return ResponseResult.success(list);
    }

}
